package com.yili.oa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.entity.TableBase;
import com.yili.oa.models.AllGame;
import com.yili.oa.models.FtGameKind;
import com.yili.oa.models.FtGamePlatform;
import com.yili.oa.service.FtGameKindService;
import com.yili.oa.service.FtGamePlatformService;
import com.yili.oa.service.GameService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.vo.request.GameDataReq;
import com.yili.oa.vo.response.GameDataRes;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/7/19 0019
 * @email 200890949@qq.com
 */
@Controller
@RequestMapping("/game")
public class GameApi {

    @Autowired
    private GameService gameService;

    @Autowired
    private FtGamePlatformService platformService;

    @Autowired
    private FtGameKindService kindService;

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/editButton/{gameId}")
    public String addButton(@PathVariable("gameId")String gameid, ModelMap modelAndView){
        SearchBase searchBase = new SearchBase();
        searchBase.setId(gameid);
        List<AllGame> list =gameService.getList(searchBase);
        AllGame Game = new AllGame();
        if (ListUtils.isNotNullOrEmpty(list)){
            Game=list.get(0);
        }
        modelAndView.addAttribute("info",Game);
        return "game/addGame";
    }

    @RequestMapping("/getList")
    @ResponseBody
    public TableBase getList(SearchBase search){
        TableBase tableBase  = new TableBase();
        PageHelper.startPage(search.getPage(),search.getLimit());
        List<AllGame> list =gameService.getList(search);
        PageInfo pageInfo = new PageInfo<>(list,search.getLimit());
        List<GameDataRes> result = new ArrayList<>();
        for (AllGame allGame:
             list) {
            GameDataRes gameDataRes = new GameDataRes();
            BeanUtils.copyProperties(allGame,gameDataRes);
            FtGameKind ftGameKind = kindService.getById(allGame.getGameKind());
            gameDataRes.setGameKindName(ftGameKind.getGameKind());
            FtGamePlatform ftGamePlatform = platformService.getById(allGame.getGamePlatform());
            gameDataRes.setGamePlatformName(ftGamePlatform.getGamePlatform());
            result.add(gameDataRes);
        }
        tableBase.setData(result);
        tableBase.setCode(0);
        tableBase.setMsg("");
        tableBase.setCount(pageInfo.getTotal());
        return tableBase;
    }

    @PostMapping("/add")
    @ResponseBody
    public String add(@RequestBody GameDataReq gameDataReq){
        List<String> list = new ArrayList<>();
        if (gameDataReq.getGameStatus()!=null){
            gameService.update(gameDataReq);
            return "success";
        }
        if (gameDataReq.getGameXima()==null){
            throw new AjaxException("请输入洗码比例");
        }
        if (gameDataReq.getGameId()==null) {
            //查名字是否重复
            Boolean result = gameService.checkName(gameDataReq.getGameName(),null);
            if (!result){
                throw new AjaxException("该游戏已存在");
            }
            gameService.add(gameDataReq);
        }else {
            Boolean result = gameService.checkName(gameDataReq.getGameName(),gameDataReq.getGameId());
            if (!result){
                throw new AjaxException("该游戏已存在");
            }
            gameService.update(gameDataReq);
        }
        return "success";
    }

    @RequestMapping("delete")
    @ResponseBody
    public String deleteGame(@RequestBody List<Long> list){
        gameService.delGame(list);
        return "success";
    }

    @RequestMapping("/getGameKind")
    @ResponseBody
    public List<FtGameKind> getGameKind(){
        List<FtGameKind> list = gameService.getGameKind();
        return list;
    }

    @RequestMapping("/getPlatform/{id}")
    @ResponseBody
    public List<FtGamePlatform> getPlatform(@PathVariable Long id){
        if (id==null){
            return null;
        }
        return gameService.getPlatform(id);
    }
}
